package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.message.bean.ContactGroup;

import java.util.ArrayList;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/7/3
 **/
public class GroupChatMemberAdapter extends BaseNormalListAdapter<ContactGroup.Member, GroupChatMemberAdapter.ViewHolder> {

    private int itemWidth;
    private boolean enableAdd, enableRemove;
    private Operate operate;

    public GroupChatMemberAdapter(Context context, int itemWidth, Operate operate) {
        super(context);
        this.itemWidth = itemWidth;
        this.operate = operate;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_message_chat_group_member, parent, false);
        switch (viewType) {
            case 1:
                return new AddViewHolder(view);
            case 2:
                return new RemoveViewHolder(view);
            default:
                return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (holder instanceof AddViewHolder) {
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (operate != null) {
                        operate.onAdd();
                    }
                }
            });
        } else if (holder instanceof RemoveViewHolder) {
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (operate != null) {
                        operate.onRemove(getDatas());
                    }
                }
            });
        } else {
            ContactGroup.Member member = getData(position);
            holder.tv.setText(member.getUsername());
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (operate != null) {
                        operate.onClick(member);
                    }
                }
            });
            GlideHelper.setCircleImage(mContext, member.getUser_img(), holder.iv, R.mipmap.icon_default_header_man);
        }
        ViewGroup.LayoutParams layoutParams = holder.iv.getLayoutParams();
        layoutParams.width = itemWidth;
        layoutParams.height = itemWidth;
        holder.iv.setLayoutParams(layoutParams);
    }

    @Override
    public int getItemCount() {
        int size = getDatas().size();
        if (enableAdd) {
            size += 1;
        }
        if (enableRemove) {
            size += 1;
        }
        return size;
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = 0;

        if (enableRemove) {
            if (position == getItemCount() - 1) {
                viewType = 2;
            }
            if (enableAdd) {
                if (position == getItemCount() - 2) {
                    viewType = 1;
                }
            }
        } else {
            if (enableAdd) {
                if (position == getItemCount() - 1) {
                    viewType = 1;
                }
            }
        }
        return viewType;
    }

    public void refresh(boolean enableAdd, boolean enableRemove, ArrayList<ContactGroup.Member> members) {
        this.enableAdd = enableAdd;
        this.enableRemove = enableRemove;
        refresh(members);
    }

    public static class RemoveViewHolder extends ViewHolder {
        RemoveViewHolder(@NonNull View itemView) {
            super(itemView);
            tv.setText("");
            iv.setImageResource(R.mipmap.icon_chat_remove_member);
        }
    }

    public static class AddViewHolder extends ViewHolder {
        AddViewHolder(@NonNull View itemView) {
            super(itemView);
            tv.setText("");
            iv.setImageResource(R.mipmap.icon_chat_add_member);
        }
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        TextView tv;
        ImageView iv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            iv = itemView.findViewById(R.id.iv);
        }
    }

    public interface Operate {
        void onAdd();

        void onRemove(ArrayList<ContactGroup.Member> members);

        void onClick(ContactGroup.Member member);
    }
}
































